package com.ruoyi.bus.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bus.domain.CustAddr;
import com.ruoyi.bus.service.ICustAddrService;
import com.ruoyi.bus.service.NoGenerateServlce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 地址簿信息
 */
@Controller
@RequestMapping("/api/custAddr")
public class FrontCustAddrController {
    @Autowired
    private NoGenerateServlce noGenerateServlce;

    @Autowired
    private ICustAddrService custAddrService;

    @RequestMapping("/list")
    @ResponseBody
    public List<CustAddr>  getList(String customerId){
        CustAddr custAddr=new CustAddr();
        custAddr.setCustomerId(customerId);
        return custAddrService.list(new QueryWrapper<>(custAddr));
    }

    @RequestMapping("/getOne")
    @ResponseBody
    public CustAddr getByAddrId(String addrId){
        return custAddrService.getOne(new QueryWrapper<>(new CustAddr().setAddrId(addrId)));
    }
    @RequestMapping("/updateAndList")
    @ResponseBody
    public List<CustAddr> updateAndList(@RequestBody CustAddr custAddr){
        if(custAddr!=null){
            custAddrService.updateById(custAddr);
            String userId=custAddr.getCustomerId();
            CustAddr custAddr1=new CustAddr();
            custAddr.setCustomerId(userId);
            return custAddrService.list(new QueryWrapper<>(custAddr1));
        }
        return null;
    }
    @RequestMapping("/addAndList")
    @ResponseBody
    public List<CustAddr> addAndList(@RequestBody CustAddr custAddr){
        if(custAddr==null){
            return custAddrService.list();
        }
        custAddr.setAddrId(noGenerateServlce.getSeqNo("AD",7));
        custAddrService.saveOrUpdate(custAddr);
        String userId=custAddr.getCustomerId();
        CustAddr custAddr1=new CustAddr();
        custAddr.setCustomerId(userId);
        return custAddrService.list(new QueryWrapper<>(custAddr1));
    }

}
